const Koa = require('koa');
const KoaRouter = require('@koa/router')
const app = new Koa();
const router = new KoaRouter();
let { list } = require('./mock')
const bodyParser = require('koa-bodyparser');
app.use(bodyParser());


router.get('/list', ctx => {
  const { q } = ctx.request.query;

  if(q) {
    ctx.body = {
      code: 200,
      data: list.filter(v => JSON.stringify(v).includes(q))
    }
  } else {
    ctx.body = {
      code: 200,
      data: list
    }
  }

 
})


router.delete('/list/:id', ctx => {
  list = list.filter(v => v.id !== ctx.request.params.id);
  ctx.body = {
    code: 200,
    message: 'ok'
  }
})


router.post('/list/:id', ctx => {
  const id = ctx.request.params.id;
  console.log(id)
  const item = list.find(v => v.id === id);
  console.log(item, ctx.request.body)
  Object.assign(item, ctx.request.body)
  ctx.body = {
    code: 200,
    message: 'ok'
  }
})



app.use(router.routes()).use(router.allowedMethods())
app.listen(7001)